package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.List;

/**
 * 套餐业务实现
 */
@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 条件查询
     *
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     *
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

    /**
     * 新增套餐
     *
     * @param setmealDTO
     */
    @Override
    public void setmealsave(SetmealDTO setmealDTO) {
        // 套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.setmealsave(setmeal);
        // 套餐关联菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
//        for (SetmealDish setmealDish : setmealDishes) {
//            setmealDishMapper.setmealDishsave(setmealDish);
//        }
        setmealDishMapper.setmealDishsave(setmealDishes);


    }

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByid(Long id) {
       /* // 根据id获得套餐信息
        Setmeal setmeal=setmealMapper.getByid(id);
        // 根据id获得与套餐相关的菜品信息
        List<SetmealDish> setmealDishes=setmealDishMapper.getBySetmealid(id);
        // 将两个的信息注入setmealDto中
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;*/
        SetmealVO setmealVO = setmealMapper.getByIdWithDish(id);
        return setmealVO;
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Override
    public void removeByid(List<Long> ids) {
       /* // 1.遍历ids
        for (Long id : ids) {
            // 2.首先查看套餐里面还有没有相关菜品 有则不能删除
            List<SetmealDish> setmealDishes = setmealDishMapper.getByid(id);
            if (setmealDishes==null||setmealDishes.size()==0){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_BE_RELATED_BY_DISH);
            }
            // 3.套餐是否启售中
            Setmeal setmeal = setmealMapper.getByid(id);
            if (setmeal.getStatus()==1){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
            // 4.删除套餐
            setmealMapper.removeByid(id);*/
        // 1.遍历ids
        for (Long id : ids) {
            // 2.套餐是否启售中
            Setmeal setmeal = setmealMapper.getByid(id);
                if (setmeal.getStatus() == 1) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
                }
            // 3.删除套餐
            setmealMapper.removeByid(id);
            setmealDishMapper.removeBySetmealid(id);
        }

    }

    /**
     * 套餐停售启售
     *
     * @param status
     * @param id
     */
    @Override
    public void upsateSetmealStatus(Integer status, Long id) {
        //1.如果套餐中含有未启售菜品则套餐不能启售
        if (status == 1) {
            List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealid(id);
            for (SetmealDish setmealDish : setmealDishes) {

                    Dish dish = dishMapper.selectById(setmealDish.getDishId());
                    if (dish.getStatus() == 0) {
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    } else {
                        setmealMapper.upsateSetmealStatus(status, id);
                    }
            }
        }
        setmealMapper.upsateSetmealStatus(status, id);
    }

    /**
     * 修改套餐信息
     *
     * @param setmealDTO
     */
    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        //1.根据id删除套餐和相关菜品

//            setmealMapper.removeByid(setmealDTO.getId());
            setmealDishMapper.removeBySetmealid(setmealDTO.getId());

        //2.重新添加
        // 套餐信息
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.updatesetmeal(setmeal);
        // 套餐关联菜品信息
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish setmealDish : setmealDishes) {
            if (setmealDish.getSetmealId()==null){
                setmealDish.setSetmealId(setmealDTO.getId());
            }
//            setmealDishMapper.setmealDishsave(setmealDish);
        }
        setmealDishMapper.setmealDishsave(setmealDishes);

//        setmealsave(setmealDTO);
   /*     Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);

        //1、修改套餐表，执行update
        setmealMapper.update(setmeal);

        //套餐id
        Long setmealId = setmealDTO.getId();

        //2、删除套餐和菜品的关联关系，操作setmeal_dish表，执行delete
        setmealDishMapper.deleteBySetmealId(setmealId);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //3、重新插入套餐和菜品的关联关系，操作setmeal_dish表，执行insert
        setmealDishMapper.insertBatch(setmealDishes);*/
    }

    /**
     * 套餐分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult setmealPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 开启分页查询  提前规定好结果要那页 这页有多少数据
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        // 下一条sql进行分页，自动加入limit关键字分页  page拦截器
        Page<SetmealVO> page = setmealMapper.setmealPage(setmealPageQueryDTO);
        //   返回 数据个数总和  根据分页查询的规定 返回这一页的集合对象
        return new PageResult(page.getTotal(), page.getResult());
    }
}
